#include "custombtn.h"
#include <QPainter>
#include <iostream>
#include <QVBoxLayout>
#include <QPropertyAnimation>

BigBtn::BigBtn(QSize size, int radius, QWidget* parent) :
    QPushButton(parent),
    cornerRadius(radius) {

    bgColor = defaultColor;
    this->resize(size);
    this->setStyleSheet(borderRadius + defaultColor);

    indicator = new QWidget(this);
    indicator->resize(this->width()/4, 6);
    indicator->move(0.375*this->width(), 0.875*this->height());
    indicator->setStyleSheet(indicatorDefaultStyle);

    QVBoxLayout* layout = new QVBoxLayout(this);
    this->setLayout(layout);
    layout->setContentsMargins(15, 35, 15, 35);
    layout->setSpacing(15);
//    layout->addWidget(indicator);
    layout->setAlignment(Qt::AlignCenter);
}


void BigBtn::enterEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);

    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.3*this->width(),
                               0.875*this->height(),
                               this->width()*0.4,
                               6));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();

    indicator->setStyleSheet(indicatorHoverStyle);
}

void BigBtn::leaveEvent(QEvent* event) {
    this->setStyleSheet(borderRadius + defaultColor);

    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(0.375*this->width(),
                               0.875*this->height(),
                               this->width()/4,
                               6));
    shorter->setDuration(150);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
    indicator->setStyleSheet(indicatorDefaultStyle);
}

void BigBtn::mousePressEvent(QMouseEvent *event) {
    this->setStyleSheet(borderRadius + pressColor);
    QPropertyAnimation* longer = new QPropertyAnimation(indicator, "geometry", this);
    longer->setStartValue(indicator->geometry());
    longer->setEndValue(QRectF(0.1*this->width(),
                               0.875*this->height(),
                               this->width()*0.8,
                               6));
    longer->setDuration(100);
    longer->setEasingCurve(QEasingCurve::OutBack);
    longer->start();
}

void BigBtn::mouseReleaseEvent(QMouseEvent* event) {
    this->setStyleSheet(borderRadius + hoverColor);
    QPropertyAnimation* shorter = new QPropertyAnimation(indicator, "geometry", this);
    shorter->setStartValue(indicator->geometry());
    shorter->setEndValue(QRectF(0.3*this->width(),
                               0.875*this->height(),
                               this->width()*0.4,
                               6));
    shorter->setDuration(100);
    shorter->setEasingCurve(QEasingCurve::OutBack);
    shorter->start();
}

void BigBtn::resizeEvent(QResizeEvent* event) {
    indicator->resize(this->width()/4, 6);
    indicator->move(0.375*this->width(), 0.875*this->height());
}
